﻿// 2025年5月17日-review_list_cpp.h: 标准系统包含文件的包含文件
// 或项目特定的包含文件。

#pragma once

#include <iostream>
using namespace std;
namespace linhui
{
	template<class T>
	struct listNode
	{
		listNode(const T& val = T())
		{
			_val = val;
		}
		//成员变量
	//private:
		T _val = 0;
		listNode* _next = nullptr;
		listNode* _prev = nullptr;
	};
	template<class T , class Ptr, class Ref>
	struct list_iterator
	{
		using Node = listNode<T>;
		using self = list_iterator<T,Ptr,Ref>;
		Node* _self;
		//构造
		list_iterator(Node* point)
		{
			_self = point;
		}
		//重载各种操作符
		Ref operator*()
		{
			return _self->_val;
		}
		self operator++()
		{
			return _self = _self->_next;
		}
		self operator++(int a)
		{
			self ret = _self;
			_self = _self->_next;
			return ret;
		}
		self operator--()
		{
			return _self = _self->_prev;
		}
		self operator--(int a)
		{
			self ret = _self;
			_self = _self->_prev;
			return ret;
		}
		bool operator!=(self right)
		{
			return _self != right._self;
		}
		bool operator==(self right)
		{
			return _self == right._self;
		}
		Ptr operator->()
		{
			return &(_self->_val);
		}
		
	};
	template<class T>
	class list
	{
	public:
		using Node = listNode<T>;
		using iterator = list_iterator<T,T*,T&>;
		using const_iterator = list_iterator<T,const T* , const T&>;
		//构造
		list()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
		template<class InputIterator>
		list(InputIterator begin, InputIterator end)
		{
			SetHead();
			while (begin != end)
			{
				push_back(*begin);
				begin++;
			}
		}
		//
		list(initializer_list<T> obj)
		{
			SetHead();
			for (auto au : obj)
			{
				push_back(au);
			}
		}
		//拷贝构造
		list(const list<T>& li)
		{
			SetHead();
			/*for (auto& au : li)
			{
				push_back(au);
			}*/
			list<T> tmp(li);
			swap(tmp);

			list<T> tmp(li.begin(),li.end());
			swap(tmp);
			
		}
		//赋值重载
		list<T>& operator=(list<T> li)
		{
			swap(li);
			return *this;
		}
		//交换函数
		void swap(list<T>& li)
		{
			std::swap(_head, li._head);
			std::swap(_size, li._size);
		}
		//析构
		~list()
		{
			clear();
			delete _head;
		}
		//数据修改
		void push_back(const T& val)
		{
			Node* newNode = new Node(val);
			Node* lastNode = _head->_prev;
			lastNode->_next = newNode;
			newNode->_prev = lastNode;
			newNode->_next = _head;
			_head->_prev = newNode;
			_size += 1;
		}
		void push_front(const T& val)
		{
			insert(begin(), val);
		}
		iterator find(const T& val)
		{
			list<T>::iterator it = begin();
			while (it != end())
			{
				if (*it == val)
				{
					return it;
				}
				it++;
			}
			return end();
		}
		iterator insert(iterator pos, const T& val)
		{
			Node* newNode = new Node(val);
			Node* cur = pos._self;
			Node* prev = cur->_prev;
			prev->_next = newNode;
			newNode->_prev = prev;
			newNode->_next = cur;
			cur->_prev = newNode;
			_size += 1;
			return newNode;
		}
		iterator erase(iterator pos)
		{
			Node* obj = pos._self;
			Node* prev = pos._self->_prev;
			Node* next = pos._self->_next;
			delete obj;
			prev->_next = next;
			next->_prev = prev;
			_size -= 1;
			return next;
		}
		void clear()
		{
			//list<T>::iterator it = begin();
			while (!empty())
			{
				pop_front();
			}
			
		}
		void pop_front() 
		{
			erase(begin());
		}
		void pop_back()
		{
			erase(--end());
		}
		//迭代器区间
		iterator begin()
		{
			return _head->_next;
		}
		iterator end()
		{
			return _head;
		}
		iterator begin() const
		{
			return _head->_next;
		}
		iterator end() const
		{
			return _head;
		}
		const_iterator rbegin()
		{
			return _head->_next;
		}
		const_iterator rend()
		{
			return _head;
		}
		const_iterator rbegin() const
		{
			return _head->_next;
		}
		const_iterator rend() const
		{
			return _head;
		}
		//容量访问
		size_t size() const
		{
			return _size;
		}
		bool empty() const
		{
			return _size == 0;
		}
		//访问头尾数据
		T& front()
		{
			return _head->_next->_val;
		}
		const T& front() const
		{
			return _head->_next->_val;
		}
		T& back()
		{
			return _head->_prev->_val;
		}
		const T& back() const
		{
			return _head->_prev->_val;
		}
		
	private:
		Node* _head = nullptr;
		size_t _size = 0;
		void SetHead()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
	};
	template<class Container>
	void PrintContainer(const Container& con)
	{
		auto it = con.begin();
		while (it != con.end())
		{
			cout << *it <<" ";
			it++;
		}
		cout << endl;
	}
}
